package com.niit.yhserver.controller;

import com.niit.yhpojo.dto.EmployeeDTO;
import com.niit.yhpojo.entity.Employee;
import com.niit.yhserver.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Slf4j
@CrossOrigin("*")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 显示全部
     */
    @GetMapping("/getAll")
    public List<Employee> getALlEmployee(){
        return employeeService.getAllEmployee();
    }

    /**
     * 按照id进行查找员工
     * @param id
     * @return
     */
    @GetMapping("/getOne/{id}")
    public Employee getOneEmployee(@PathVariable("id") Integer id){
        return employeeService.findOneById(id);
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping("/addEmp")
    public String addEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}",employeeDTO);
        employeeService.insert(employeeDTO);
        return "新增员工成功！";
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
        employeeService.deleteById(id);
        return "删除员工成功！";
    }

    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping("/updateEmp")
    public String updateEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息:{}",employeeDTO);
        employeeService.update(employeeDTO);
        return "编辑员工信息成功！";
    }



}
